﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using System.IO;
using System.Data;
using V5_EShop.Data;

namespace V5_EShop.Admin {
    public partial class Admin_Friend : AdminPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                Action = InputHelper.SetString(Request["Action"]);
                this.Bind_DropDownList();
                switch (Action) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        this.Bind_DataList();
                        break;
                }
            }
        }

        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;

            int RecordCount = 0;
            DataTable dt = V5.SQL.Query_Pager("*", "(Select B.ClassTitle,A.* From Tbl_Friend A Left Join Tbl_FriendClass B On A.ClassId=B.ClassId) AA", "", "Id Desc", Pager.PageSize, Pager.CurrentPageIndex, ref RecordCount);
            this.Pager.RecordCount = RecordCount;
            this.gvList.DataSource = dt;
            this.gvList.DataBind();
        }

        private void Bind_DropDownList() {
            DataTable dt = V5.Class.GetDropDownList("Tbl_FriendClass", 0, "请选择");
            this.ddlClassId.DataSource = dt;
            this.ddlClassId.DataTextField = "ClassTitle";
            this.ddlClassId.DataValueField = "ClassId";
            this.ddlClassId.DataBind();
        }

        private void Bind_DataEdit(int id) {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelFriend model = new ModelFriend(p => p.ID == id);
            if (model != null) {
                this.txtID.Value = model.ID.ToString();
                this.ddlClassId.SelectedIndex = this.ddlClassId.Items.IndexOf(
                        this.ddlClassId.Items.FindByValue(model.ClassId.Value.ToString())
                    );
                this.rblLinkType.SelectedIndex = this.rblLinkType.Items.IndexOf(
                    this.rblLinkType.Items.FindByValue(model.LinkType.Value.ToString())
                );
                this.txtFriendTitle.Text = model.FriendTitle;
                this.txtFriendUrl.Text = model.FriendUrl;
                this.txtFriendLogo.Text = model.FriendLogo;
                this.txtFriendReadme.Text = model.FriendReadme.ToString();
                this.rbtnListIsShow.SelectedIndex = this.rbtnListIsShow.Items.IndexOf(
                        this.rbtnListIsShow.Items.FindByValue(model.IsShow.Value.ToString())
                 );
                this.btnSave.Text = "修改保存";
                this.lblTitle.Text = "添加友情链接";
            }
        }

        private void Bind_Delete(int id) {
            new ModelFriend(p => p.ID == id).Delete();
            this.Bind_DataList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            int ID = InputHelper.SetNumber(this.txtID.Value);
            int ClassId = InputHelper.SetNumber(this.ddlClassId.SelectedValue);
            int LinkType = InputHelper.SetNumber(this.rblLinkType.SelectedValue);
            string FriendTitle = this.txtFriendTitle.Text;
            string FriendUrl = this.txtFriendUrl.Text;
            string FriendLogo = this.txtFriendLogo.Text;
            string FriendReadme = this.txtFriendReadme.Text;
            int IsShow = InputHelper.SetNumber(this.rbtnListIsShow.SelectedValue);
            ModelFriend model = ID == 0 ? new ModelFriend() : new ModelFriend(p => p.ID == ID);
            model.ClassId = ClassId;
            model.LinkType = LinkType;
            model.FriendTitle = FriendTitle;
            model.FriendUrl = FriendUrl;
            #region 上传图片
            if (this.fuFriendLogo.PostedFile.ContentLength > 0) {
                try {
                    string fullFilePath = this.fuFriendLogo.FileName;
                    string fileName = Path.GetFileName(fullFilePath);//文件名
                    string FileDate = DateTime.Now.ToString("yyyy-MM");
                    string saveFileName = "/UploadFiles/Friend/" + FileDate + "/" + fileName;
                    string savePath = "UploadFiles/Friend/" + FileDate + "/";
                    string UploadFilesPath = Server.MapPath("~/");
                    if (!Directory.Exists(UploadFilesPath + savePath)) {
                        Directory.CreateDirectory(UploadFilesPath + savePath);
                    }
                    UploadFilesPath += saveFileName;
                    this.fuFriendLogo.SaveAs(UploadFilesPath);
                    model.FriendLogo = saveFileName;
                }
                catch (Exception ex) {
                    model.FriendLogo = "";
                }
            }
            else {
                model.FriendLogo = FriendLogo;
            }
            #endregion
            model.FriendReadme = FriendReadme;
            model.IsShow = IsShow;
            if (ID == 0) {
                model.Add();
            }
            else if (ID > 0) {
                model.ID = ID;
                model.Update();
            }
            this.Bind_DataList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            int id = InputHelper.SetNumber(e.CommandArgument);
            switch (eName.Trim().ToLower()) {
                case "go":
                    this.Bind_DataEdit(id);
                    break;
                case "del":
                    this.Bind_Delete(id);
                    break;
                default:
                    break;
            }
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            this.Bind_DataList();
        }

    }
}
